package com.jymf.consume.service;

import com.jymf.consume.dao.ClientBalanceDao;
import com.jymf.consume.dao.ClientConsumeRecordDao;
import com.jymf.consume.dao.JymfClientInfoDao;
import com.jymf.model.ClientBalance;
import com.jymf.model.ClientConsumeRecord;
import com.jymf.model.JymfClientInfo;
import com.jymf.model.Page;
import org.apache.log4j.spi.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zyj
 * @create 2017-09-25
 **/
@Service("ConsumeClientService")
public class ClientService {

    Logger logger = org.slf4j.LoggerFactory.getLogger(ClientService.class);

    @Autowired
    private JymfClientInfoDao jymfClientInfoDao;
    @Autowired
    private ClientBalanceDao clientBalanceDao;
    @Autowired
    private ClientConsumeRecordDao clientConsumeRecordDao;

    public List<JymfClientInfo> getClientInfo(Page<JymfClientInfo> page){
        try {
            return  jymfClientInfoDao.getClientInfoPage(page);
        } catch (Exception e) {
            e.printStackTrace();
            return  null;
        }
    }

    public JymfClientInfo getInfo(int clientId) throws Exception{
        return jymfClientInfoDao.getInfo(clientId);
    }

    public void updateClientById(JymfClientInfo jymfClientInfo) throws Exception{
        jymfClientInfoDao.updateByPrimaryKeySelective(jymfClientInfo);
    }

    /**
     * 保存客户信息和金额
     * @param jymfClientInfo
     * @throws Exception
     */
    @Transactional
    public void saveClientInfo (JymfClientInfo jymfClientInfo) throws Exception{
        jymfClientInfoDao.insertSelective(jymfClientInfo);

        ClientBalance clientBalance = new ClientBalance();
        clientBalance.setAddDate(new Date());
        clientBalance.setClientinfoId(jymfClientInfo.getClientId());
        clientBalanceDao.insertSelective(clientBalance);
    }

    public List<Map<String, Object>> getBalanceList(Page<Map<String, Object>> page){
        return clientBalanceDao.getBalanceInfo(page);
    }

    public List<Map<String, Object>> getClientRecordList(Page<Map<String, Object>> page){
        return clientConsumeRecordDao.getClientRecordList(page);
    }

    //修改金额
    @Transactional
    public Map<String, Object> clientCharge(ClientBalance record, ClientConsumeRecord clientConsumeRecord, Map<String, Object> resultMap) throws Exception{
        boolean b = false;
        String info = "";
        //step1:判断是否有该用户
        JymfClientInfo jymfClientInfo = jymfClientInfoDao.getInfo(record.getClientinfoId());
        if(jymfClientInfo==null){
            resultMap.put("type", b);
            resultMap.put("info", "没有该用户");
            return  resultMap;
        }

        //step2:判断该用户是否启用
        if(jymfClientInfo.getIsUse()==null||"2".equals(jymfClientInfo.getIsUse())){
            resultMap.put("type", b);
            resultMap.put("info", "该用户没有启用");
            return  resultMap;
        }

        //step3:充值
        recharge(record, clientConsumeRecord);
        b = true;
        resultMap.put("info", "充值成功");
        resultMap.put("type", b);
        return  resultMap;
    }

    public void recharge(ClientBalance record, ClientConsumeRecord clientConsumeRecord) throws Exception{
        clientConsumeRecordDao.insertSelective(clientConsumeRecord);
        clientBalanceDao.chargeByClientId(record);
    }


    //修改金额
    public Map<String, Object> clientConsume(ClientBalance record, ClientConsumeRecord clientConsumeRecord, Map<String, Object> resultMap) throws Exception{
        boolean b = false;
        String info = "";
        //step1:判断是否有该用户
        JymfClientInfo jymfClientInfo = jymfClientInfoDao.getInfo(record.getClientinfoId());
        if(jymfClientInfo==null){
            resultMap.put("type", b);
            resultMap.put("info", "没有该用户");
            return  resultMap;
        }

        //step2:判断该用户是否启用
        if(jymfClientInfo.getIsUse()==null||"2".equals(jymfClientInfo.getIsUse())){
            resultMap.put("type", b);
            resultMap.put("info", "该用户没有启用");
            return  resultMap;
        }

        //step3:消费
        consume(record, clientConsumeRecord);
        b = true;
        resultMap.put("info", "扣款成功");

        resultMap.put("type", b);
        return  resultMap;
    }


    public void consume(ClientBalance record, ClientConsumeRecord clientConsumeRecord) throws Exception{
        clientBalanceDao.consumeByClientId(record);
        clientConsumeRecordDao.insertSelective(clientConsumeRecord);
    }

    public ClientBalance selectBalanceByClientId(int clientId){
        return clientBalanceDao.selectByClientId(clientId);
    }

}
